<!--- Takes results from Java XML parser and creates structure of all elements --->
<cfparam name="attributes.xml_data" default="">
<cfparam name="attributes.returnvar" default="x">
<cfparam name="attributes.arraylist" default=""><!--- to have a group of elements returned as array, specify level of the xml in the following format: "shopping_cart.line_item,configuration.component" --->
<cfparam name="attributes.jurisdiction_flag" default="false">
<cfset tmpstruct = structnew()>

<cfif isarray(attributes.xml_data)>
	<cfloop from="1" to="#arraylen(attributes.xml_data)#" index="x">

		<!--- For a complex (nested) xml structure, Recursively call itself to get to the deepest layer --->
		<cfif structkeyexists(attributes.xml_data[x],"xmlchildren") and arraylen(attributes.xml_data[x].xmlchildren)>
		
			<cf_xml_to_struct xml_data="#attributes.xml_data[x].xmlchildren#" arraylist="#attributes.arraylist#" returnvar="y"> 
			<cfset "tmpstruct.#attributes.xml_data[x].XmlName#" = y>
				
			<!--- Create any arrays specified in calling list (arraylist parameter) --->
			<cfif len(attributes.arraylist)>
				<cfloop list="#attributes.arraylist#" index="m">
					<cfif attributes.xml_data[x].XmlName eq listfirst(m,".")>
						<cfset "tmpstruct.#listfirst(m,".")#.#listlast(m,".")#" = arraynew(1)>
						<!---	go through the number of elements in the MX struct, recursively call xml_to_struct for each element, assign results back to array --->
						<cfloop from="1" to="#arraylen(attributes.xml_data[x].xmlchildren)#" index="n">
							<!--- <cfif listlast(m,".") eq 'suborder'><cfdump var="#attributes.xml_data[x]#"></cfif> --->
							<cfif structkeyexists(attributes.xml_data[x].xmlchildren[n],"xmlchildren")>
								<cfif findnocase(listlast(m,"."),attributes.xml_data[x].xmlchildren[n].xmlname)>
									<cfif arraylen(attributes.xml_data[x].xmlchildren[n].xmlchildren)>
										<cf_xml_to_struct xml_data="#attributes.xml_data[x].xmlchildren[n].xmlchildren#" arraylist="#attributes.arraylist#" returnvar="y">
									<cfelse>
										<cfset y = attributes.xml_data[x].xmlchildren[n].xmltext>
									</cfif>
									<cfset var.temp = arrayappend(evaluate("tmpstruct." & listfirst(m,".") & "." & listlast(m,".")),y)>
								</cfif>
							</cfif>
						</cfloop>
					</cfif>
				</cfloop>
			</cfif>
			
		<cfelse>
			<cfset "tmpstruct.#attributes.xml_data[x].XmlName#" = attributes.xml_data[x].xmltext>
		</cfif>
	</cfloop>
	<cfset temp = setvariable('caller.' & attributes.returnvar, tmpstruct)>

<cfelseif attributes.jurisdiction_flag eq "true"><!--- T129541: GET_JURISDICTION POST ONLY --->
	<cfloop from="1" to="#arraylen(attributes.xml_data)#" index="x">
		<!--- For a complex (nested) xml structure, Recursively call itself to get to the deepest layer --->
		<cfif structkeyexists(attributes.xml_data[x],"xmlchildren") and arraylen(attributes.xml_data[x].xmlchildren)>
			<cf_xml_to_struct xml_data="#attributes.xml_data[x].xmlchildren#" arraylist="#attributes.arraylist#" returnvar="y"> 
			<cfset "tmpstruct.#attributes.xml_data[x].XmlName#" = y>
		<cfelse>
			<cfset "tmpstruct.#attributes.xml_data[x].XmlName#" = attributes.xml_data[x].xmltext>
		</cfif>
	</cfloop>
	<cfset temp = setvariable('caller.' & attributes.returnvar, tmpstruct)>
</cfif>

<cfsetting enablecfoutputonly="no">
